$(document).ready(function() {
	//ajaxform
	var options = { 
		beforeSubmit:showRequest,//表单验证方式
        success:showResponse  // post-submit callback 
    }; 

    $('#ajaxForm').ajaxForm(options); 

    function showResponse(responseText, statusText)  {
		if (statusText == 'success') {
			var responseText=eval('('+responseText+')');
			if(responseText.state == 'SUCCESS'){
				formDialog.content(responseText.message);
				formDialog.title('SUCCESS');
				formDialog.icon('succeed');
				var timer,i=2;
				var fn = function () {
					formDialog.title(i + ' seconds after close');
					if (!i){
						formDialog.close();
						clearInterval(timer);
						if(responseText.url==''){
							window.location.reload();
						}else{
							window.location.href = responseText.url;	
						}
					}
					i --;
				};
				timer = setInterval(fn, 1000);

			}else{
				formDialog.content(responseText.state);
				formDialog.title('error message');
				formDialog.icon('error');
			}			
		}else{
			formDialog.content('Lost Service');
			formDialog.title('error message');			
		}
	} 

	function showRequest(){
		 validata = $("form.form-validata").valid();
		 if (validata){
		 	formDialog = art.dialog({
				title : 'information',
			  content : '<img src="/tpl/Admin/common/image/loadding.gif">',
			     lock : true
			})
		 }
		 return validata;
	}

	//validation验证
	$("form.form-validata").validate({
		errorElement: "span",
        errorClass: "help-inline error",
	})

	$.validator.addMethod("pwd",function(value,element){
        return this.optional(element) || testPass(value);
    },"Password must be at least 8 characters and include at least 3 of the following requirement<br/>Contain at least 1 English uppercase chatacters (A-Z)<br/>Contain at least 1 English lowercase characters (a-z)<br/>Contain at least 1 numeric base 10 digits (0-9)<br/>Contain at least 1 non-alphanumeric characters (for example: ?,!,&,%)");

    $.validator.addMethod("phone",function(value,element){
        return this.optional(element) || testPhone(value);
    },"Phone format error");

});


function uploadImage(url , domid){
	art.dialog.data('domid', domid);
	art.dialog.open(url,{
	lock:true,
	title:'upload',
	width:500,
	height:300,
	});
}

function delImage(domid){
	$("#"+domid).val("");
	$("#"+domid+"_src").attr('src','/tpl/static/image/image.jpg');
}


function CheckAll(form){
  for (var i=0;i<form.elements.length;i++){
    var e = form.elements[i];
  	if (e.name != 'chkall'){
	e.checked = form.checkall.checked;
	}
   }
}

function openWindow( title , url , domid , width, height){
	art.dialog.data('domid', domid);
	art.dialog.open(url,{
	title:title,
	lock:true,
	width:width,
	height:height,
	});
}

//密码
function testPass(str){
    var rC = {
        lW:'[a-z]',
        uW:'[A-Z]',
        nW:'[0-9]',
        sW:'[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E]'
    };
    function Reg(str, rStr){
        var reg = new RegExp(rStr);
        if(reg.test(str)) return true;
        else return false;
    }
    if(str.length < 8){
        //document.title = '您的密码长度太短';
        return false;
    }else{
        var tR = {
            l:Reg(str, rC.lW),
            u:Reg(str, rC.uW),
            n:Reg(str, rC.nW),
            s:Reg(str, rC.sW)
        };
        if((tR.l && tR.u && tR.n) || (tR.l && tR.u && tR.s) || (tR.s && tR.u && tR.n) || (tR.s && tR.l && tR.n)){
            //document.title = '密码符合要求';
            return true;
        }else{
            //document.title = '您的密码必须含有“小写字母”、“大写字母”、“数字”、“特殊符号”中的任意三种';
            return false;
        }
    }
}

function testPhone(v){
    if(v==""){
        return false;
    }else{
        var pattern = /^((?:\d{9})|(?:\d{3}-\d{4})|(?:\d{3}-\d{3}-\d{4})|[0-9]{10})$/;
        flag = pattern.test(v);
        if(!flag){
            return false;
        }else{
            return true;
        }
    }
}